class Allocator {
    public:
        Allocator(int n) : _n(n) {}
    
        int allocate(int size, int mID) {
            int oldright = 0;
            for (auto i = window.begin(); i != window.end(); i++) {
                int left = i->first;
                // cout << left <<' ' << oldright << endl;
                if (left >= oldright + size) {
                    window[oldright] = {size, mID};
                    return oldright;
                }
                oldright = left + i->second.first;
            }
            if (_n >= oldright + size) {
                window[oldright] = {size, mID};
                return oldright;
            }
            return -1;
        }
    
        int freeMemory(int mID) {
            int ans = 0;
            for (auto i = window.begin(); i != window.end(); i++) {
                if (i->second.second == mID) {
                    int tmp = i->second.first;
                    window.erase(i->first);
                    ans += tmp;
                }
            }
            return ans;
        }
        int _n = 0;
        map<int, pair<int, int>> window;
    };
    
    /**
     * Your Allocator object will be instantiated and called as such:
     * Allocator* obj = new Allocator(n);
     * int param_1 = obj->allocate(size,mID);
     * int param_2 = obj->freeMemory(mID);
     */